package com.evildog.workspace.file.controller;

import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.file.bo.VirtualFileBO;
import com.evildog.workspace.file.convert.VirtualFileConvert;
import com.evildog.workspace.file.service.UploadService;
import com.evildog.workspace.file.vo.VirtualFileVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 公用的上传
 */
@RestController
@RequestMapping("/uploads")
@AllArgsConstructor
@Api(tags = "公用上传接口")
public class UploadController {

    private final UploadService uploadService;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "folder", value = "上传后的文件放置的文件夹", example = "common"),
            @ApiImplicitParam(name = "save", value = "是否同时保存至数据库", allowableValues = "0,1", defaultValue = "0")
    })
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<VirtualFileVO> upload(@RequestParam(value = "file") MultipartFile file,
                                        @RequestParam("folder") String folder,
                                        @RequestParam(value = "save", defaultValue = "0") boolean save) {
        VirtualFileBO upload = uploadService.upload(file, folder, save);
        return ResultGenerator.success(VirtualFileConvert.INSTANCE.convert(upload));
    }


}
